package com.ciwong.epaper.modules.wordlist;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.widget.RoundProgressBar;

/**
 * Created by young  on 2018/3/28.
 */

public class WordListBottomView extends LinearLayout {

    private RoundProgressBar round_progress_bar;
    private ImageView iv_playing, iv_recording, yin_pin_load;
    private TextView repeat_btn,next_word_btn;
    private ViewGroup ls_timer_layout;
    public  static final int  UPDATAPROGRESS=1;



    public WordListBottomView(Context context) {
        super(context);
        initView();
    }

    public WordListBottomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public WordListBottomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView(){
        setOrientation(LinearLayout.VERTICAL);
        setBackgroundColor(getResources().getColor(R.color.white));
        View.inflate(getContext(), R.layout.word_list_bottom,this);
        round_progress_bar=(RoundProgressBar) findViewById(R.id.round_progress_bar);
        iv_playing =(ImageView) findViewById(R.id.problemPlay);
        iv_recording =(ImageView) findViewById(R.id.img_micro_recording);
        yin_pin_load =(ImageView) findViewById(R.id.yin_pin_load);
        iv_playing =(ImageView) findViewById(R.id.problemPlay);
        repeat_btn=findViewById(R.id.repeat_btn);
        next_word_btn=findViewById(R.id.next_word_btn);
        ls_timer_layout=findViewById(R.id.ls_timer_layout);
    }


    /**
     * 设置播放图标的显示状态
     *
     * @param isShow
     */
    public void setPlayBtnState(boolean isShow) {
        if (iv_playing == null) {
            return;
        }
        if (isShow) {
            iv_playing.setVisibility(View.VISIBLE);
            Drawable drawable = iv_playing.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable) {
                ((AnimationDrawable) drawable).start();
            }

        } else {
            iv_playing.setVisibility(View.GONE);
            Drawable drawable = iv_playing.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable) {
                ((AnimationDrawable) drawable).stop();
            }
         }
    }
    public void setWordLoadState(boolean isLoading){
        if (yin_pin_load == null) {
            return;
        }

        ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(yin_pin_load,"rotation",0f,360f);
        objectAnimator.setDuration(1000);
        objectAnimator.setRepeatCount(ObjectAnimator.RESTART);

        if(isLoading){
            yin_pin_load.setVisibility(VISIBLE);
            objectAnimator.start();
        }else {
            objectAnimator.cancel();
            yin_pin_load.setVisibility(GONE);
        }


    }

    public void showNextBtn(boolean isShow,boolean islast){
        if(isShow){
            ls_timer_layout.setVisibility(VISIBLE);
        }else {
            ls_timer_layout.setVisibility(INVISIBLE);
        }
        if(islast){
            next_word_btn.setText("完成");
        }else {
            next_word_btn.setText("下一个");
        }
    }

    //设置录音图标的显示状态
    public void setRecordState(boolean isRecording) {

        if (null == iv_recording) {
            return;
        }

        if (isRecording) {
            iv_recording.setVisibility(VISIBLE);
            Drawable drawable = iv_recording.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable) {
                ((AnimationDrawable) drawable).start();
            }
        }else {
            iv_recording.setVisibility(View.GONE);
            Drawable drawable = iv_recording.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable) {
                ((AnimationDrawable) drawable).stop();
            }
        }
    }

    public void setRoundprogressMax(int max){
        round_progress_bar.setMax(max);
    }

    public void setRoundProgress(int progress){
        round_progress_bar.setProgress(progress);
    }
    //播放录音倒计时
    public void setRoundProgressAndMax(int max,int p){
        setRoundprogressMax(max);
        setRoundProgress(p);
    }


    //设置倒计时图标显示状态
    public void setRoundRrogressBarState(boolean isShow){
        if(isShow){
            round_progress_bar.setVisibility(View.VISIBLE);
        }else {
            round_progress_bar.setVisibility(View.GONE);
        }
    }

    //设置加载动画显示和隐藏
    public void SetLoadingState(boolean isShow){
        if(isShow){
            yin_pin_load.setVisibility(View.VISIBLE);
        }else {
            yin_pin_load.setVisibility(View.GONE);
        }
    }

    /*
     *初始状态
     */
    public void setInitState(){
        setRoundRrogressBarState(false);
        setPlayBtnState(false);
        setRecordState(false);
        SetLoadingState(false);

    }
    //设置播放状态下的图标显示
    public void payWordsState(){

        setRoundRrogressBarState(false);
        setRecordState(false);
        setPlayBtnState(true);
        SetLoadingState(false);

    }

    //播放提示音时图标状态  或者   正常播放时无图标页面
    public void playHintState(){
        setRoundRrogressBarState(false);
        setRoundProgress(0);
        setPlayBtnState(false);
        setRecordState(false);
        SetLoadingState(false);
    }
    //录音状态图标状态
    public void recordingState(){

        setPlayBtnState(false);
        setRecordState(true);
        setRoundProgress(0);
        setRoundRrogressBarState(true);
        SetLoadingState(false);
    }
    //自动更新进度
    public void autoUpdateProgress(final int timer){
        round_progress_bar.setTimer(timer);
    }
}
